<template>
  <div class="employees-container">
    <div class="app-container">
      <overall>
        <template #left>
          <span>总记录数: {{total}} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="dialogVisible=true">新增员工</el-button>
        </template>
      </overall>

      <el-card style="margin-top: 10px;" >
        <el-table border :data="list" :default-sort = "{prop: 'timeOfEntry', order: 'descending'}">
          <el-table-column label="序号"  type="index"/>
          <el-table-column label="姓名" prop = "username"/>
          <el-table-column label="工号"   sortable prop="workNumber"/>
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default={row}>{{hireForm(row.formOfEmployment)}}</template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName">
          </el-table-column>
          <el-table-column label="入职时间"   sortable prop="timeOfEntry" />
          <!-- <el-table-column label="账户状态" /> -->
          <el-table-column label="操作" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hDele(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
         <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[1, 2, 5, 40]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
        </el-row>
      </el-card>

      <!-- 弹框 -->
      <el-dialog
  title="新增员工"
  :visible.sync="dialogVisible" @close="reset">
  <empDialog @close="dialogVisible=false" ref="ref" @renderer="renderer"></empDialog>
  </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments,delDepartments } from "@/api/departments";
import empDialog from "@/views/employees/empDialog";
import hire from "@/constant/employees";
const hi = {}
hire.hireType.forEach(item=>{ 
  hi[item.id]=item.value
 })
export default {
  components:{
empDialog
  },
  data() {
    return {
      page:1,//页码
      size:2,//条数
      list:[],
      total:0,
      dialogVisible:false,//显示隐藏
    };
  },
  created(){
    this.loadDepartments()
  },
  methods:{
    // 删除
   async hDele(id){
      if(!confirm('确定要删除吗')) return  this.$message.error('取消删除')
      try {
        await delDepartments(id)
        this.loadDepartments()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error);
      }
    },
    // 重置'
    reset(){
      this.$refs.ref.reset()
      this.dialogVisible=false
    },
    // 渲染页面
    renderer(){
      this.total++
      this.page=Math.ceil(this.total/this.size)
      this.loadDepartments()
      this.dialogVisible=false
    },
    // 分页
    handleSizeChange(val) {
        // console.log(`每页 ${val} 条`);
        this.page=1
        this.size=val
        this.loadDepartments()
      },
      handleCurrentChange(val) {
        // console.log(`当前页: ${val}`);
        this.page=val
        this.loadDepartments()
      },
    // 枚举使用
    hireForm(id){
    // return  hire.hireType.find(item=> item.id == id).value
    return hi[id]
    },
    // 获取员工列表
  async loadDepartments(){
    try {
      const res = await getDepartments(this.page,this.size)
      // console.log(res);
      this.list = res.data.rows
      this.total = res.data.total
    } catch (error) {
      console.log(error);
    }
  }
  }
}
</script>

<style>

</style>